// 班级管理器
export class ClassManager {
  static instance = null
  
  constructor() {
    this.classes = [
      { id: 1, name: '计算机1班', grade: '2021', major: '计算机科学与技术', studentCount: 30 },
      { id: 2, name: '软件2班', grade: '2022', major: '软件工程', studentCount: 25 },
    ]
    
    this.students = {
      1: [ // 班级ID: 1 的学生列表
        { id: 1, studentId: '2021001', name: '张三', grade: '2021', major: '计算机科学与技术' },
        { id: 2, studentId: '2021002', name: '李四', grade: '2021', major: '计算机科学与技术' },
        { id: 3, studentId: '2021003', name: '王五', grade: '2021', major: '计算机科学与技术' },
        { id: 4, studentId: '2021004', name: '赵六', grade: '2021', major: '计算机科学与技术' },
        { id: 5, studentId: '2021005', name: '钱七', grade: '2021', major: '计算机科学与技术' },
      ],
      2: [ // 班级ID: 2 的学生列表
        { id: 6, studentId: '2022001', name: '孙八', grade: '2022', major: '软件工程' },
        { id: 7, studentId: '2022002', name: '周九', grade: '2022', major: '软件工程' },
        { id: 8, studentId: '2022003', name: '吴十', grade: '2022', major: '软件工程' },
      ]
    }
  }
  
  static getInstance() {
    if (!this.instance) {
      this.instance = new ClassManager()
    }
    return this.instance
  }
  
  // 班级相关操作
  getClasses() {
    return this.classes
  }
  
  getClassById(id) {
    return this.classes.find(cls => cls.id === id) || null
  }
  
  addClass(classData) {
    const newId = this.classes.length > 0 ? Math.max(...this.classes.map(c => c.id)) + 1 : 1
    const newClass = { 
      ...classData, 
      id: newId,
      studentCount: 0
    }
    this.classes.push(newClass)
    this.students[newId] = []
    return newClass
  }
  
  updateClass(classData) {
    const index = this.classes.findIndex(c => c.id === classData.id)
    if (index !== -1) {
      // 保留学生数量
      const studentCount = this.students[classData.id]?.length || 0
      this.classes[index] = { ...classData, studentCount }
      return true
    }
    return false
  }
  
  deleteClass(id) {
    const index = this.classes.findIndex(c => c.id === id)
    if (index !== -1) {
      this.classes.splice(index, 1)
      // 删除该班级的学生数据
      delete this.students[id]
      return true
    }
    return false
  }
  
  // 学生相关操作
  getStudents(classId) {
    return this.students[classId] || []
  }
  
  getStudentById(classId, studentId) {
    return this.students[classId]?.find(s => s.id === studentId) || null
  }
  
  addStudent(classId, student) {
    if (!this.students[classId]) {
      this.students[classId] = []
    }
    
    // 检查学号是否重复
    if (this.students[classId].some(s => s.studentId === student.studentId)) {
      return { success: false, message: '该学号已存在' }
    }
    
    const newId = this.students[classId].length > 0 
      ? Math.max(...this.students[classId].map(s => s.id)) + 1 
      : 1
      
    const newStudent = { ...student, id: newId }
    this.students[classId].push(newStudent)
    
    // 更新班级的学生数量
    const classIndex = this.classes.findIndex(c => c.id === classId)
    if (classIndex !== -1) {
      this.classes[classIndex].studentCount = this.students[classId].length
    }
    
    return { success: true, student: newStudent }
  }
  
  addStudents(classId, students) {
    if (!this.students[classId]) {
      this.students[classId] = []
    }
    
    // 检查学号重复
    const existingStudentIds = this.students[classId].map(s => s.studentId)
    const newStudents = []
    const duplicateStudents = []
    
    students.forEach(student => {
      if (!existingStudentIds.includes(student.studentId) && 
          !newStudents.some(s => s.studentId === student.studentId)) {
        newStudents.push(student)
      } else {
        duplicateStudents.push(student)
      }
    })
    
    // 添加新学生
    if (newStudents.length > 0) {
      const startId = this.students[classId].length > 0 
        ? Math.max(...this.students[classId].map(s => s.id)) + 1 
        : 1
        
      newStudents.forEach((student, index) => {
        this.students[classId].push({
          id: startId + index,
          ...student
        })
      })
      
      // 更新班级的学生数量
      const classIndex = this.classes.findIndex(c => c.id === classId)
      if (classIndex !== -1) {
        this.classes[classIndex].studentCount = this.students[classId].length
      }
    }
    
    return {
      success: true,
      added: newStudents.length,
      duplicates: duplicateStudents.length
    }
  }
  
  updateStudent(classId, student) {
    if (!this.students[classId]) return false
    
    const index = this.students[classId].findIndex(s => s.id === student.id)
    if (index !== -1) {
      // 检查学号是否与其他学生重复
      const otherStudents = this.students[classId].filter(s => s.id !== student.id)
      if (otherStudents.some(s => s.studentId === student.studentId)) {
        return { success: false, message: '该学号已存在' }
      }
      
      this.students[classId][index] = { ...student }
      return { success: true }
    }
    return { success: false, message: '学生不存在' }
  }
  
  deleteStudent(classId, studentId) {
    if (!this.students[classId]) return false
    
    const index = this.students[classId].findIndex(s => s.id === studentId)
    if (index !== -1) {
      this.students[classId].splice(index, 1)
      
      // 更新班级的学生数量
      const classIndex = this.classes.findIndex(c => c.id === classId)
      if (classIndex !== -1) {
        this.classes[classIndex].studentCount = this.students[classId].length
      }
      
      return true
    }
    return false
  }
} 